package com.yangle.frame.bizlogic.controller;

import com.yangle.frame.bizlogic.entity.Article;
import com.yangle.frame.bizlogic.entity.Tages;
import com.yangle.frame.bizlogic.service.ArticleService;
import com.yangle.frame.bizlogic.service.TagesService;
import com.yangle.frame.common.controller.BaseController;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import tk.mybatis.mapper.util.StringUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * Created by yangle on 2018/5/26.
 */
@Controller
@RequestMapping("/bizlogic/article")
public class ArticleController extends BaseController<Article>{
    @Autowired
    private ArticleService articleService;
    @Autowired
    private TagesService tagesService;
    @RequestMapping("/list")
    public String list(HttpServletRequest request, Article entity) {
        super.list(request, entity, articleService);
        return "bizlogic/article/list";
    }

    @RequestMapping("/edit")
    public String edit(HttpServletRequest request) {
        super.edit(request, articleService);
       String id= request.getParameter("id");
        if(StringUtil.isNotEmpty(id)){
        Tages tages=new Tages();
        tages.setArticleID(id);
            List<Tages> temp=tagesService.getAll(tages);
            List<String> tagValue=new ArrayList<>();
            for(Tages tages1:temp){
                tagValue.add(tages1.getTagName());
            }

        request.setAttribute("tages", StringUtils.join(tagValue,","));
        }
        return "bizlogic/article/edit";
    }


    @RequestMapping("/save")
    public String save(HttpServletRequest request,Article entity,String tags) {
        entity.setTag(tags);
        if (StringUtil.isNotEmpty(entity.getId())) {
            super.update(request, entity, articleService);
        } else {
            entity.setId(UUID.randomUUID().toString());
            super.create(request, entity, articleService);
        }
        if(StringUtil.isNotEmpty(tags)){
            String[] tagList=tags.split(",");
            Tages tags1=new Tages();
            tagesService.deleteByArticleId(entity.getId());
            for(String tag:tagList){
                tags1.setArticleID(entity.getId());
                tags1.setTagName(tag);
                tags1.setId(UUID.randomUUID().toString());
                tagesService.create(tags1);
            }
        }
        //内容置顶，清除之前所有的置顶标记
        if("true".equals(entity.getIsZd())){
articleService.updateAllZD(entity.getId());
        }
        return "common/result";
    }

    @RequestMapping("/delete")
    public String delete(HttpServletRequest request) {
        super.delete(request, articleService);
        return "common/result";
    }

    @RequestMapping("/view")
    public String view(HttpServletRequest request) {
        super.view(request, articleService);
        return "bizlogic/article/view";
    }
}